#include <stdio.h> 
#include <stdlib.h> 
#include "timer.h"
#include "usb_device.h"
#include "usbd_cdc.h"
//#include "usmart.h"
#include "malloc.h"
#include <ctype.h>						 /* Character functions                */
#include "math.h"
#include "fsmc.h"
#include "../PUBLIC/Gamma_dll.h"

int X_enable,Y_enable,Z_enable;
USBD_HandleTypeDef *USB_Link;
double Target_lum_off,Target_x_off,Target_y_off;//预设精度
double Target_lum,Target_x,Target_y; //预设标准Lxy
double Target_X,Target_Y,Target_Z; //预设标准XYZ
int Target_gray;

double L_X,L_Y,L_Z;
double measure_X,measure_Y,measure_Z;
double CASDK_LUM,CASDK_X,CASDK_Y;
bool CASDK_Busy;
int Gamma_auto_adjust_count_set;
int Gamma_reg_range;
int Input_reg_red, Input_reg_green, Input_reg_blue;
static int Tuning_busy =0; //=1,调节中
int Node_index =0;

void Msr_CASDK(void)
{
	buffer[0]=0x90;
	buffer[1]=0x00;
	buffer[2]=0x0A;
	STM2PC(USB_Link,CDC_IN_EP,buffer,buffer[2]+3);
	CASDK_Busy=false;
}

void F_SET_PATTERN(int color)
{
	if(OLED.SigMode == Video_Mode)
	{ 
		FSMC_Pure_colour((u32)(color<<16)|((u32)(color<<8)|color));
		delay_ms(2);
		FSMC_Pure_colour((u32)(color<<16)|((u32)(color<<8)|color));
		delay_ms(5);
		FSMC_Pure_colour((u32)(color<<16)|((u32)(color<<8)|color));
		Load_done_HIGH;
		SSD2828_W_Reg(OLED.SigMode,1,0xB7,HS_B7_Data);
	}
	else if(OLED.SigMode == CMD_Mode)    
	{
		SSD2828_W_Reg(OLED.SigMode,1,0xB7,HS_B7_Data);
		delay_ms(5); 
		FSMC_Pure_colour((u32)(color<<16)|((u32)(color<<8)|color));
		SSD2828_W_Reg(OLED.SigMode,1,0xB7,LP_B7_Data);	
		delay_ms(5);	
	}
}


void F_SET_TYPICAL_GAMMA_SPEC(int Point,double Lum,double X,double Y,double lum_off,double x_off,double y_off ,double Curve)
{       
		Target_lum = pow((double)(Point/ 255.0), Curve) * Lum; //每绑点目标亮度
		Target_x=X;
		Target_y=Y;
		Lvxy_to_XYZ(Target_lum, Target_x, Target_y);//X/Z/Z转换
		Target_X = L_X; //R/B/G分亮度计算
		Target_Y = L_Y;
		Target_Z = L_Z;
		Target_lum_off=lum_off;
		Target_x_off=x_off;
		Target_y_off=y_off;
}


void F_CHECK_GAMMA_SPEC(void)
{
	if (Gamma_adjust_timeout_flag == true)//如果是0绑点
	{
		X_enable = 0;
		Y_enable = 0;
		Z_enable = 0;
	}
	else
	{
		if ((CASDK_LUM < (Target_lum + Target_lum * Target_lum_off / 100)) && (CASDK_LUM > (Target_lum - Target_lum * Target_lum_off / 100)))//G
		{ Y_enable = 0; }
		else
		{ Y_enable = 1; }
		if ((CASDK_X < (Target_x + Target_x_off)) && (CASDK_X > (Target_x - Target_x_off)) && Y_enable == 0)//R
		{ X_enable = 0; }
		else
		{ X_enable = 1; }
		if ((CASDK_Y < (Target_y + Target_y_off)) && (CASDK_Y > (Target_y - Target_y_off)) && X_enable == 0 && Y_enable == 0)//B
		{ Z_enable = 0; }
		else
		{ Z_enable = 1; }
	}
}


void F_GAMMA_TUNING(void)
{
	bool ret;
	Lvxy_to_XYZ(CASDK_LUM, CASDK_X, CASDK_Y);//X/Y/Z转换
	measure_X=L_X;
	measure_Y=L_Y;
	measure_Z=L_Z;
	
	Flexible_Gamma_auto_Adujst(Y_enable, X_enable,Z_enable,measure_Y, measure_X,measure_Z,Target_Y, Target_X, Target_Z,
	Output_reg_red, Output_reg_green, Output_reg_blue,Target_gray, Gamma_reg_range,Gamma_auto_adjust_count_set );

	F_CHECK_GAMMA_SPEC();	
	if(Gamma_auto_adjust_count >= Gamma_auto_adjust_count_set || Gamma_adjust_timeout_flag==true || (X_enable==0&&Y_enable==0&&Z_enable==0))
	{
		Tuning_busy=0;
		Node_index++;
	}
	else
		Tuning_busy=1;
}

void F_GAMMA_PREPARE(int gray,double tlum,double tx,double ty,double lum_off,double x_off,double y_off ,double Curve,int input_reg_red,int input_reg_green,int input_reg_blue,int reg_range,int tuning_times)
{
	Gamma_adjust_timeout_flag = false;
	Gamma_auto_adjust_count = 0;
	Gamma_auto_adjust_count_set=tuning_times;
	Gamma_reg_range=reg_range;
	Input_reg_red=input_reg_red;
	Input_reg_green=input_reg_green;
	Input_reg_blue=input_reg_blue;
	Output_reg_red=input_reg_red;
	Output_reg_green=input_reg_green;
	Output_reg_blue=input_reg_blue;
	Target_gray=gray;
	F_SET_TYPICAL_GAMMA_SPEC(gray, tlum, tx, ty, lum_off, x_off, y_off , Curve);	
	X_enable=1;
	Y_enable=1;
	Z_enable=1;
	Tuning_busy=1;
}


void EE_Design_Key0(USBD_HandleTypeDef *pdev)
{	
	USB_Link=pdev;
	//使能定时器3和定时器3更新中断：TIM_IT_UPDATE    
	HAL_TIM_Base_Start_IT(&TIM3_Handler); //启动调节
	Node_index=0;
}

void EE_Design_Key1(USBD_HandleTypeDef *pdev)
{
	HAL_TIM_Base_Stop_IT(&TIM3_Handler); 
}


void RM692EX_INTER_GAMMA(void)
{
	u8 buffer1[5];
	if(OLED.SigMode == Video_Mode)
	{
	SSD2828_W_Reg(OLED.SigMode,1,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
	}
	else if(OLED.SigMode == CMD_Mode)	
	{
		SSD2828_W_Reg(OLED.SigMode,1,0xB7,LP_B7_Data);
	}
	delay_ms(5);
	buffer1[0] = 0x02;
	buffer1[1] = 0xFE;
	buffer1[2] = 0x70;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0);  			
	buffer1[1] = 0x28;
	buffer1[2] = 0x00;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0); ////Disable gamma int
	buffer1[1] = 0xFE;
	buffer1[2] = 0x40;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0); 	
	buffer1[1] = 0xDA;
	buffer1[2] = 0xC8;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0);	////Enable measure  mode //15point	
	buffer1[1] = 0xD7;
	buffer1[2] = 0x01;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0);	////gamma  x		
	buffer1[1] = 0xFE;
	buffer1[2] = 0x78;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0);	
	buffer1[1] = 0xF4;
	buffer1[2] = 0xC0;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0);		
	buffer1[1] = 0xF5;
	buffer1[2] = 0x01;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0);	////gamma  x	
	SSD2828_W_Reg(OLED.SigMode,1,0xB7,HS_B7_Data);
}


 u8 RM692EX_Gamma_R[54]=
{
	0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,
	0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2d,0x2f,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37	
};
 u8 RM692EX_Gamma_G[54]=
{
	0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,0x65,0x66,
	0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e
};
 u8 RM692EX_Gamma_B[54]=
{
	0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,0xa0,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa9,0xaa,0xab,0xac,
	0xad,0xae,0xaf,0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,0xc0,0xc1,0xc2,0xc3,0xc4
};



void RM692EX_GammaW(void)
{
	u8 buffer1[3];
	
	if(OLED.SigMode == Video_Mode)
	SSD2828_W_Reg(OLED.SigMode,1,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
	else if(OLED.SigMode == CMD_Mode)	
	SSD2828_W_Reg(OLED.SigMode,1,0xB7,LP_B7_Data);
	delay_ms(5);	

	buffer1[0] = 0x02;			
	buffer1[1] = 0xFE;
	buffer1[2] = 0x50;
	SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
	
	if(Target_gray==255)
	{
		buffer1[0] = 0x02;	
		buffer1[1] = 0x1c;   //R
		buffer1[2] = 0x0f&(Output_reg_red>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0x1d;   //R
		buffer1[2] = Output_reg_red;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);

		buffer1[1] = 0x65;   //G
		buffer1[2] = 0x0f&(Output_reg_green>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0x66;   //G
		buffer1[2] = Output_reg_green;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		
		buffer1[1] = 0xab;   //B
		buffer1[2] = 0x0f&(Output_reg_blue>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0xac;   //B
		buffer1[2] = Output_reg_blue;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
	}
	else if(Target_gray==239)
	{
		buffer1[0] = 0x02;	
		buffer1[1] = 0x1a;   //R
		buffer1[2] = 0x0f&(Output_reg_red>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0x1b;   //R
		buffer1[2] = Output_reg_red;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);

		buffer1[1] = 0x63;   //G
		buffer1[2] = 0x0f&(Output_reg_green>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0x64;   //G
		buffer1[2] = Output_reg_green;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		
		buffer1[1] = 0xa9;   //B
		buffer1[2] = 0x0f&(Output_reg_blue>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0xaa;   //B
		buffer1[2] = Output_reg_blue;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
	}
	else if(Target_gray==207)
	{
		buffer1[0] = 0x02;	
		buffer1[1] = 0x18;   //R
		buffer1[2] = 0x0f&(Output_reg_red>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0x19;   //R
		buffer1[2] = Output_reg_red;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);

		buffer1[1] = 0x61;   //G
		buffer1[2] = 0x0f&(Output_reg_green>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0x62;   //G
		buffer1[2] = Output_reg_green;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		
		buffer1[1] = 0xa7;   //B
		buffer1[2] = 0x0f&(Output_reg_blue>>8);
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
		buffer1[1] = 0xa8;   //B
		buffer1[2] = Output_reg_blue;
		SSD2828_W_Array(OLED.SigMode,1,buffer1,0);
	}	
	delay_ms(5);
	SSD2828_W_Reg(OLED.SigMode,1,0xB7,HS_B7_Data);
	delay_ms(5);
}



void F_AUTO_GAMMA(int gray,double tlum,double tx,double ty,double lum_off,double x_off,double y_off ,double Curve,int input_reg_red,int input_reg_green,int input_reg_blue,int reg_range,int tuning_times)
{
	HAL_TIM_Base_Stop_IT(&TIM3_Handler); 
	if(Tuning_busy==0)
	{
		RM692EX_INTER_GAMMA();
		F_SET_PATTERN(gray);
		F_GAMMA_PREPARE(gray, tlum, tx, ty, lum_off,x_off,y_off ,Curve,input_reg_red,input_reg_green,input_reg_blue,reg_range,tuning_times);
	}
	RM692EX_GammaW();
	Msr_CASDK();
	while(CASDK_Busy==false){delay_ms(1);};
	F_GAMMA_TUNING();
	HAL_TIM_Base_Start_IT(&TIM3_Handler); 
}

//定时器3中断服务函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if(htim==(&TIM3_Handler))
    {	
		switch(Node_index)
		{
			case 0:
				F_AUTO_GAMMA(255,430,0.3,0.315,1,0.001,0.001,2.2,0x8ff,0x8ff,0x8ff,0xFFF,70);
			break;
//			case 1:
//				F_AUTO_GAMMA(239,430,0.3,0.315,1,0.001,0.001,2.2,0x7ff,0x7ff,0x7ff,0xFFF,50);	
//			break;
//			case 2:
//				F_AUTO_GAMMA(207,430,0.3,0.315,1,0.001,0.001,2.2,0x6ff,0x6ff,0x6ff,0xFFF,50);	
//			break;
//			case 3:
//				F_AUTO_GAMMA(147,430,0.3,0.315,1,0.001,0.001,2.2,0x6ff,0x6ff,0x6ff,0xFFF,50);	
//			break;
//			case 4:
//				F_AUTO_GAMMA(111,430,0.3,0.315,1,0.001,0.001,2.2,0x6ff,0x6ff,0x6ff,0xFFF,50);	
//			break;
//			case 5:
//				F_AUTO_GAMMA(79,430,0.3,0.315,1,0.001,0.001,2.2,0x6ff,0x6ff,0x6ff,0xFFF,50);	
//			break;
			
			default:
				break;
		}
    }
}